package com.itheima.stock.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserServise;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVo;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiseImpl implements UserServise{

    private static final int CAPTCHA_SIZE = 4;

    private final IdWorkers idWorkers;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private Cache cache;

    public UserServiseImpl(IdWorkers idWorkers) {
        this.idWorkers = idWorkers;
    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        //1.参数校验
        if(ObjectUtils.isEmpty(loginDTO)
                || ObjectUtils.isEmpty(loginDTO.getUsername())
                || ObjectUtils.isEmpty(loginDTO.getPassword())
                || ObjectUtils.isEmpty(loginDTO.getCode())
                || ObjectUtils.isEmpty(loginDTO.getRkey())){
            //参数异常 参数不能为空
            Exceptions.cast(CodeEnum.PARAM_IS_NOT_NULL);
        }
        //2.核心登陆逻辑、
        //2.1 校验验证码
        String captchaInRedis = cache.get(loginDTO.getRkey());
        if(!StringUtils.equals(captchaInRedis,loginDTO.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        //2.2 根据用户名 用户状态 是否删除 查询用户信息
//        LambdaQueryWrapper<SysUser> lambdaQuery = Wrappers.lambdaQuery();
//        SysUser user = baseMapper.selectOne(lambdaQuery
//                        .eq(SysUser::getUsername,loginDTO.getUsername())
//                        .eq(SysUser::getDeleted,Constants.SYS_USER_STATUS_NORMAL)
//                        .eq(SysUser::getStatus,Constants.SYS_USER_UN_DELETED ));
        SysUser user = sysUserMapper.selectByUsername(loginDTO.getUsername(),Constants.SYS_USER_STATUS_NORMAL,Constants.SYS_USER_UN_DELETED);


        //2.2 判断用户是否存在 密码是否正确
        if(ObjectUtils.isEmpty(user) || !passwordEncoder.matches(loginDTO.getPassword(),user.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_PASSWORD_ERROR);
        }

        //返回
        return LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .nickName(user.getNickName())
                .phone(user.getPhone())
                .build();
    }

    @Override
    public CaptchaVo getCaptcha() {
        //随机生成四位验证码
        String captcha = RandomStringUtils.randomNumeric(CAPTCHA_SIZE);
        //自动生成key
        String captchaKey = RedisKeyUtil.getCaptchaKey(Constants.STOCK_CAPTCHA_PREFIX,idWorkers.nextStrId());

        //设置缓存
        cache.set(captchaKey,captcha,Constants.CAPTCHA_EXPIRE_TIME);

        return CaptchaVo.builder()
                .code(captcha)
                .rkey(captchaKey)
                .build();
    }


}
